package sax;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.log4j.Logger;

public class ToXML {
	private static String NL = System.getProperty("line.separator");
	private FileWriter fileWriter;
	private Logger logger = Logger.getLogger(ToXML.class);

	/**
	 * @param pathForFile
	 * @throws IOException
	 */
	public ToXML(String pathForFile) throws IOException {
		File file = new File(pathForFile);
		fileWriter = new FileWriter(file);
		fileWriter.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
				+ NL);
		fileWriter.append("<tables>" + NL);
		fileWriter.flush();

	}

	public String toXMLFirstTable(ResultSet rs) throws SQLException {
		Object value = null;
		StringBuffer xml = new StringBuffer();
		xml.append("<!-- BEGIN Table People -->" + NL);
		xml.append("<People>" + NL);

		while (rs.next()) {
			xml.append("<person>");
			value = rs.getObject(2);
			xml.append("<name>" + value.toString().trim() + "</name>");
			value = rs.getObject(3);
			xml.append("<pid>" + value.toString().trim() + "</pid>");
			value = rs.getObject(4);
			xml.append("<age>" + value.toString().trim() + "</age>");
			value = rs.getObject(5);
			xml.append("<email>" + value.toString().trim() + "</email>");
			xml.append("</person>\n");
		}
		xml.append("</People>" + NL);
		xml.append("<!-- END Table People -->" + NL);
		return xml.toString();
	}

	public String toXMLSecondTable(ResultSet rs) throws SQLException {
		Object value = null;
		StringBuffer xml = new StringBuffer();
		xml.append("<!--BEGIN Table Trip -->" + NL);
		xml.append("<Trips>" + NL);

		while (rs.next()) {
			xml.append("<trip>");
			value = rs.getObject(2);
			xml.append("<city>" + value.toString().trim() + "</city>");
			value = rs.getObject(3);
			xml.append("<ticketNumber>" + value.toString().trim()
					+ "</ticketNumber>");
			value = rs.getObject(4);
			xml.append("<arriveAt>" + value.toString().trim() + "</arriveAt>");
			value = rs.getObject(5);
			xml.append("<departAt>" + value.toString().trim() + "</departAt>");
			xml.append("</trip>" + NL);
		}
		xml.append("</Trips>" + NL);
		xml.append("<!--END Table Trip -->" + NL);
		return xml.toString();
	}

	public String toXMLThirdTable(ResultSet rs) throws SQLException {
		Object value = null;
		StringBuffer xml = new StringBuffer();
		xml.append("<!-- BEGIN Table carshop -->" + NL);
		xml.append("<carshop>" + NL);

		while (rs.next()) {
			xml.append("<car>");
			value = rs.getObject(2);
			xml.append("<model>" + value.toString().trim() + "</model>");
			value = rs.getObject(3);
			xml.append("<year>" + value.toString().trim() + "</year>");
			xml.append("</car>" + NL);
		}
		xml.append("</carshop>" + NL);
		xml.append("<!-- END Table carshop -->" + NL);
		return xml.toString();
	}

	public void toFile(String infoForFile) {

		try {

			fileWriter.append(infoForFile);
			fileWriter.flush();

		} catch (IOException e) {
			logger.error(e);
		}

	}

	public void doOnExit() throws IOException {
		fileWriter.append("</tables>" + NL);
		fileWriter.flush();
		fileWriter.close();
	}

}
